<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('预算、预测、实际回款周汇总')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse" shiro:hasAnyRoles="admin,ccm,financial">
                <form id="formId">
                    <div class="select-list" >
                        <ul>
                            <li id="personName" shiro:hasAnyRoles="admin,ccm,financial">
                                <p>销售人员：</p>
                                <input type="text" name="personName" />
                            </li>
                            <li id="deptName" shiro:hasAnyRoles="admin,financial">
                                <p>所属部门：</p>
                                <input type="text" name="deptName" />
                            </li>
                            <li id="contractName">
                                <p>项目名称：</p>
                                <input type="text" name="contractName" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">

                <a class="btn btn-success" onclick="initPersonal()" shiro:hasAnyRoles="admin,ccm,sale,financial">
                    <i class="fa fa-plus"></i> 个人级
                </a>
                <a class="btn btn-success" onclick="initDept()" shiro:hasAnyRoles="admin,ccm,financial">
                    <i class="fa fa-plus"></i> 部门级
                </a>
                <a class="btn btn-success" onclick="initTotal()" shiro:hasAnyRoles="admin,financial">
                    <i class="fa fa-plus"></i> 公司级
                </a>
                <!--<a class="btn btn-success" onclick="initContract()">-->
                    <!--<i class="fa fa-plus"></i> 项目级-->
                <!--</a>-->
            </div>
            <div class="col-sm-12 select-table table-striped table-responsive">
                <table id="bootstrap-table"></table>
                <div>公司级：公司所有项目；部门级：部门所有项目；个人级：销售个人负责所有项目；项目级：罗列出所有项目。</div>
                <div>预算：每周的预算累加；</div>
                <div>预测：每周的预测累加（当前周及历史周所有预测数据已经定版，未来周预测数据可酌情调整）；</div>
                <div>实际：每周的实际回款累加（以财务到账为准）；</div>
                <div>预测准确度 = 每周的实际回款 / 预测回款数；</div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/weekly";
        var week = [[${week}]];
        var scope;
        function initTotal(){
            $.table.destroy();
            scope = 'total'
            showSearchList(scope);
            var columns =  new Array();
            for(let i=0; i<=54; i++){
                let column = new Object();
                if(i==0){
                    column.field = 'type';
                    column.title = '公司级';
                }else if(i<54){
                    column.field = i;
                    column.title = 'W' + i;
                    if( i == week){
                        column.cellStyle = {css:{'background':'#F0E68C'}};
                    }
                    column.formatter = function(value, row, index) {
                        if (index % 5 ==3 || index % 5 ==4) {
                            return value;
                        } else {
                            return [
                                '<a href="#" onclick="pivot(' + index + ',' + i + ',\'' + row.name + '\')">' + value + '</a>'
                            ].join("")
                        }
                    }
                }else{
                    column.field = i;
                    column.title = '合计';
                }
                columns.push(column);
            }
            var options = {
                url: prefix + "/totalList",
                exportUrl: prefix + "/export",
                modalName: "预算、预测、实际回款周汇总",
                rowStyle: rowStyle,//通过自定义函数设置行样式
                pagination: false,
                fixedColumns: true,
                fixedNumber: 1,
                // rightFixedColumns:true,
                // rightFixedNumber:1,
                search: false,
                showSearch:false,
                showFooter: true,
                showToggle: false,
                showExport: true,
                exportOptions : {
                    ignoreColumn: [],
                    fileName : '预算预测回款周汇总',
                    worksheetName : '预算预测回款周汇总（公司级）'
                },
                columns: columns
            };
            $.table.init(options);
        }

        function initPersonal(){
            $.table.destroy();
            scope = 'personal'
            showSearchList(scope);
            var columns =  new Array();
            for(let i=-1; i<=54; i++){
                let column = new Object();
                if(i==-1){
                    column.field = 'name';
                    column.title = '名称';
                }else if(i==0){
                    column.field = 'type';
                    column.title = '个人级';
                }else if(i<54){
                    column.field = i;
                    column.title = 'W' + i;
                    if( i == week){
                        column.cellStyle = {css:{'background':'#F0E68C'}};
                    }
                    column.formatter = function(value, row, index) {
                        if (index % 5 ==3 || index % 5 ==4) {
                            return value;
                        } else {
                            return [
                                '<a href="#" onclick="pivot(' + index + ',' + i + ',\'' + row.name + '\')">' + value + '</a>'
                            ].join("")
                        }
                    }
                }else{
                    column.field = i;
                    column.title = '合计';
                }
                columns.push(column);
            }
            var options = {
                url: prefix + "/personalList",
                exportUrl: prefix + "/export",
                modalName: "预算、预测、实际回款周汇总",
                rowStyle: rowStyle,//通过自定义函数设置行样式
                pagination: false,
                showSearch:true,
                showFooter: true,
                showToggle: false,
                showExport: true,
                exportOptions : {
                    ignoreColumn: [],
                    fileName : '预算预测回款周汇总',
                    worksheetName : '预算预测回款周汇总（个人级）'
                },
                columns: columns,
                onLoadSuccess: function (data) {
                    mergeRowspans('#bootstrap-table',0,['name'],5,data.total);
                }
            };
            $.table.init(options);
        }

        function initDept(){
            $.table.destroy();
            scope = 'dept'
            showSearchList(scope);
            var columns =  new Array();
            for(let i=-1; i<=54; i++){
                let column = new Object();
                if(i==-1){
                    column.field = 'name';
                    column.title = '名称';
                }else if(i==0){
                    column.field = 'type';
                    column.title = '部门级';
                }else if(i<54){
                    column.field = i;
                    column.title = 'W' + i;
                    if( i == week){
                        column.cellStyle = {css:{'background':'#F0E68C'}};
                    }
                    column.formatter = function(value, row, index) {
                        if (index % 5 ==3 || index % 5 ==4) {
                            return value;
                        } else {
                            return [
                                '<a href="#" onclick="pivot(' + index + ',' + i + ',\'' + row.name + '\')">' + value + '</a>'
                            ].join("")
                        }
                    }
                }else{
                    column.field = i;
                    column.title = '合计';
                }
                columns.push(column);
            }
            var options = {
                url: prefix + "/deptList",
                exportUrl: prefix + "/export",
                modalName: "预算、预测、实际回款周汇总",
                rowStyle: rowStyle,//通过自定义函数设置行样式
                pagination: false,
                showSearch:true,
                showFooter: true,
                showToggle: false,
                showExport: true,
                exportOptions : {
                    ignoreColumn: [],
                    fileName : '预算预测回款周汇总',
                    worksheetName : '预算预测回款周汇总（部门级）'
                },
                columns: columns,
                onLoadSuccess: function (data) {
                    mergeRowspans('#bootstrap-table',0,['name'],5,data.total);
                }
            };
            $.table.init(options);
        }

        // function initContract(){
        //     $.table.destroy();
        //     showSearchList('contract');
        //     var columns =  new Array();
        //     for(let i=-1; i<54; i++){
        //         let column = new Object();
        //         if(i==-1){
        //             column.field = 'name';
        //             column.title = '名称';
        //         }else if(i==0){
        //             column.field = 'type';
        //             column.title = '项目级';
        //         }else{
        //             column.field = i;
        //             column.title = 'W' + i;
        //         }
        //         columns.push(column);
        //     }
        //     var options = {
        //         url: prefix + "/contractList",
        //         exportUrl: prefix + "/export",
        //         modalName: "预算、预测、实际回款周汇总",
        //         rowStyle: rowStyle,//通过自定义函数设置行样式
        //         pagination: false,
        //         showSearch:true,
        //         showFooter: true,
        //         showToggle: false,
        //         showExport: true,
        //         exportOptions : {
        //             ignoreColumn: [],
        //             fileName : '预算预测回款周汇总',
        //             worksheetName : '预算预测回款周汇总（项目级）'
        //         },
        //         columns: columns,
        //         onLoadSuccess: function (data) {
        //             console.log(data);
        //             mergeRowspans('#bootstrap-table',0,['name'],4,data.total);
        //         }
        //     };
        //     $.table.init(options);
        // }

        function rowStyle(row, index) {
            var classes = ['active', 'success', 'info', 'warning', 'danger'];
            if ((index + 1) % 5 === 0 || (index + 2) % 5 === 0) {
                return {
                    classes: classes[1]
                };
            }
            return {};
        }

        function mergeRowspans(targetTable, merIndex,fieldArr,rowSum,totalRow) {
            var count = totalRow/rowSum;
            for (var j = 0; j < count; j++){
                for (var i = 0;i < fieldArr.length;i++){
                    $(targetTable).bootstrapTable('mergeCells', {index: j*rowSum, field: fieldArr[i], rowspan: rowSum});
                }
            }
        }

        function showSearchList(id){
            if(id == 'total'){
                $('.search-collapse').hide();
            }else if(id == 'dept'){
                $('.search-collapse').show();
                $('#deptName').show();
                $('#personName').hide();
                $('#contractName').hide();
            }else if(id == 'personal'){
                $('.search-collapse').show();
                $('#deptName').hide();
                $('#personName').show();
                $('#contractName').hide();
            }else if(id == 'contract'){
                $('.search-collapse').show();
                $('#deptName').hide();
                $('#personName').hide();
                $('#contractName').show();
            }
        }

        $(function() {
            initPersonal();
        });

        function pivot(index, i, name){
            name = name == 'undefined' ? '' : name;
            var level = scope == 'total' ? '公司级' : (scope == 'dept' ? '部门级' : '个人级');
            var type;
            if(index % 5 == 0)
                type = '预算';
            if(index % 5 == 1)
                type = '预测';
            if(index % 5 == 2)
                type = '实际';
            var title = name + level + '第' + i + '周' + type + '回款表';

            var options = {
                title: title,
                width: "960",
                url: prefix + "/weeklyPivotTable?name=" + name + "&type=" + type + "&week=" + i + "&level=" + level
            };
            $.modal.openOptions(options);
        }
    </script>
</body>
</html>